from .base import Base
from .common import CommonMixin
from sqlalchemy import String, Integer, SmallInteger, ForeignKey, func, text, Column, DateTime, Text
from sqlalchemy.orm import relationship

""" 用户表 """


class User(Base, CommonMixin):
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True, comment='主键ID', autoincrement=True)
    name = Column(String(60), unique=True, comment="用户名")
    password = Column(String(64), comment="密码")
    avatar = Column(Text, comment="头像")
    sex = Column(SmallInteger, server_default=text('0'), comment="性别: 0 未知 1 男 2 女")
    phone = Column(String(30), comment="手机号")
    version = Column(Integer, server_default=text('1'), comment="版本号")
    user_role = relationship("UserRole", back_populates="user", lazy=False)
    nickname = Column(String(255), comment="昵称")
    action_id = Column(String(255), comment="操作人id")
    status = Column(Integer, comment='是否已经删除', default=0)

    def schema(self):
        return {
            'id': self.id,
            'username': self.name,
            'hashed_password': self.password,
            'avatar': self.avatar,
            'sex': self.sex,
            'phone': self.phone,
            "status": self.status,
            "nickname": self.nickname,
            'creattime': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'updatetime': self.update_time.strftime('%Y-%m-%d %H:%M:%S'),
        }
